Apache CXF 使用教程 - 02 - Maven

Apache CXF 使用 - 工具 - Maven

添加依赖

要使用Maven来进行CXF开发,在POM文件中添加依赖:

<properties>
<cxf.version>3.1.8</cxf.version>
</properties>
<dependencies>
<!--Java API for XML Web Services -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<!--Java API for RESTful Web Services -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<!--JSON for RESTful Web Services -->
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.8.8</version>
</dependency>
</dependencies>

额外依赖

根据CXF的使用,添加需要的依赖:

<!-- Use dependency blocks for these CXF artifact Ids just as above -->
cxf-rt-core
cxf-rt-frontend-simple
cxf-rt-frontend-jaxws
cxf-rt-databinding-aegis
cxf-rt-transports-local
cxf-rt-transports-http
cxf-rt-transports-http-jetty
cxf-rt-transports-jms
cxf-rt-management
cxf-common-utilities

Maven Snapshot Repository

若需使用最新版本的CXF,可以添加 Apache 的Snapshot仓库:

<repositories>
...other repos...
<repository>
<id>apache-snapshots</id>
<name>Apache SNAPSHOT Repository</name>
<url>http://repository.apache.org/snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
...other repos...
<pluginRepository>
...same repo as above...
</pluginRepository>
</pluginRepositories>

插件仓库(plugin repositories )部分是必须的,因为用于WSDL2Java, Java2WS等任务的 cxf-codegen-plugin需要从地址下载。.

Maven Java2WS 插件

此插件用于从 Java 类生成 WSDL相关代码, 包括用于启动web service 的服务端代码和客户端代码.
插件的POM内容:

<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-java2ws-plugin</artifactId>
<version>${cxf.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-simple</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>process-classes</id>
<phase>process-classes</phase>
<configuration>
<className>org.apache.hello_world.Greeter</className>
<genWsdl>true</genWsdl>
<verbose>true</verbose>
</configuration>
<goals>
<goal>java2ws</goal>
</goals>
</execution>
</executions>
</plugin>

选项及其默认值:

<configuration>
<className>...</className>
<classpath>...</classpath>
<outputFile>...</outputFile>
<genWsdl>true</genWsdl>
<genServer>false</genServer>
<genClient>false</genClient>
<genWrapperbean>false</genWrapperbean>
<frontend>jaxws</frontend>
<databinding>jaxb</databinding>
<serviceName>...</serviceName>
<soap12>false</soap12>
<targetNameSpace>...</targetNameSpace>
<verbose>false</verbose>
<quiet>false</quiet>
<attachWsdl>true</attachWsdl>
<address>...</address>
</configuration>

更详细的配置查看 Java to WS页面.
生成的文件默认为:
${project.build.directory}/generated/wsdl/${className}.wsdl

Maven cxf-codegen-plugin (WSDL to Java)

CXF 包含一个从 WSDL生成Java代码的插件,其maven配置为:

<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/myService.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>

本例中在 maven 的generate-sources阶段(phase)运行了 wsdl2java 目标(goal). 通过运行 mvn generate-sources, CXF 将在<sourceRoot>目录生成指定的构件( artifacts). 每个 <wsdlOption> 元素与一个用于生成构件的WSDL 相对应。WSDL的位置由<wsdl> 选项指定. 根据 Maven 的标准目录结构, 如果想将WSDL打包到生成的JAR包之中, 需要将上述WSDL 放在 /src/main/resources/下(若不想放到JAR的根目录下,可以将其放在resources下的子文件夹中); 或者放在/src/main/config 文件夹中使 WSDL 不包括在JAR中.

下面的例子表明了一些自定义的选项. 默认条件下, codegen plugin 遵从 Maven 约定将生成的类代码放在"target/generated-sources/cxf" 输出文件夹。 可使用 <sourceRoot> 来覆盖默认配置, 但是通常不必. 其他配置参数可包括在<wsdlOption>元素中. 这些参数将被传递给插件及WSDL的java页面中的相关选项。

<configuration>
<sourceRoot>${project.build.directory}/generated-code/mywebservice</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl>
<!-- you can set the options of wsdl2java command by using the <extraargs> -->
<extraargs>
<extraarg>-impl</extraarg>
<extraarg>-verbose</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>

打开博客 (英文)来查看cxf-codegen-plugin生成的完整的 service 和 client 例子 .

例子 1: 生成 JAX-WS Binding 文件

<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl>
<bindingFiles>
<bindingFile>${basedir}/src/main/resources/async_binding.xml</bindingFile>
</bindingFiles>
</wsdlOption>
</wsdlOptions>
</configuration>

本例中我们指定了一个想要在CXF中使用的 JAX-WS 绑定文件。绑定文件( Binding files)是一种定制CXF生成的构件的输出的方式。 例如,其允许我们修改CXF使用的包名(package name)
例子 2: 指定绑定的数据

<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl>
<extraargs>
<extraarg>-databinding</extraarg>
<extraarg>jibx</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>

In this example we’re specifying that we want CXF to use our data binding jibx. You can also using the data binding of xmlbeans, domsources, sdo etc.

例子 3: 指定生成构件的目标 service

<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl>
<serviceName>MyWSDLService</serviceName>
</wsdlOption>
</wsdlOptions>
</configuration>

In this example we’re specifying that we only want to generate artifacts for the service named “MyWSDLService” in the WSDL.

为了避免在多个<wsdlOption>中黏贴复制,可以声明一个<defaultOption>元素.

例子 4: 使用 defaultOption来避免重复

<configuration>
<defaultOptions>
<bindingFiles>
<bindingFile>${basedir}/src/main/jaxb/bindings.xml</bindingFile>
</bindingFiles>
<noAddressBinding>true</noAddressBinding>
</defaultOptions>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl>
<serviceName>MyWSDLService</serviceName>
</wsdlOption>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/myOtherService.wsdl</wsdl>
<serviceName>MyOtherWSDLService</serviceName>
</wsdlOption>
</wsdlOptions>
</configuration>

<defaultOption><wsdlOption> 对应的选项在 WSDL to Java 的页面中列出, 可以查看 http://svn.apache.org/repos/asf/cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/Option.java 了解更多信息.

At least, you can declare a common wsdlRoot folder where you store your WSDL files and use includes/excludes patterns to select the files to get used by the code generator

例子 5: 使用 wsdlRoot 和 includes/excludes 模式

<configuration>
<defaultOptions>
<bindingFiles>
<bindingFile>${basedir}/src/main/jaxb/bindings.xml</bindingFile>
</bindingFiles>
<noAddressBinding>true</noAddressBinding>
</defaultOptions>
<wsdlRoot>${basedir}/src/main/resources/wsdl</wsdlRoot>
<includes>
<include>*Service.wsdl</include>
</includes>
</configuration>

wsdlRoot 的默认值是src/main/resources/wsdl ,因此可以忽略此选项。.

例子 6: 从maven仓库加载 wsdl
对 CXF 2.3 及之后的版本有一个新的配置元素 <wsdlArtifact> 可以用来从仓库加载wsdl.

<configuration>
<wsdlOptions>
<wsdlOption>
<wsdlArtifact>
<groupId>org.apache.pizza</groupId>
<artifactId>PizzaService</artifactId>
<version>1.0.0</version>
</wsdlArtifact>
</wsdlOption>
</wsdlOptions>
</configuration>

上述信息将会加载 wsdl/org/apache/pizza/PizzaService-1.0.0.wsdl到本地maven仓库,并根据它生成java 代码。

例子 7: 使用xjc扩展
通过 <extraarg> 元素可以定制标准的JAXB命令行, 每行一条命令或使用逗号(comma)分割。 CXF 同时也为代码生成提供了一些 JAXB 扩展. 它们可以作为依赖加入,然后一个包含内容 -xjc-X<extension id>的额外参数(extraarg )来激活。

artifact iddescriptionextension id
cxf-xjc-booleanAdds getters for booleansboolean
cxf-xjc-bug671Workaroung for JAXB bug 671bug671
cxf-xjc-dvDefault value supportdv
cxf-xjc-tsAdds toString to objectsts
cxf-xjc-wsdlextensionWsdlExtension supportwsdlextension

jaxb-fluent-api|Fluent API for setters|fluent-api|

An example showing attachment of a JAXB binding file and the CXF toString() extension is below:

<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl>
<extraargs>
<extraarg>-xjc-b,binding.xjb</extraarg>
<extraarg>-xjc-Xts</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.cxf.xjcplugins</groupId>
<artifactId>cxf-xjc-ts</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
</plugin>

Example 8 - Using JAXB/JAX-WS 2.2 with Java 6
Java 6 includes JAXB/JAX-WS 2.1 API’s and a 2.1 implementations. However, sometimes it’s desirable to use JAXB or JAX-WS 2.2 instead to obtain various bug fixes and enhancements. Using 2.2 with Java 6 and Maven can be a bit tricky as it requires endorsing the API jars which requires configuration of a bunch of plugins, requires use of “forking”, etc… First off, both Surefire and the Compiler plugins need to be setup to point at an endorsed dir:

<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<endorseddirs>${project.build.directory}/endorsed</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>once</forkMode>
<argLine>-Djava.endorsed.dirs=${project.build.directory}/endorsed</argLine>
</configuration>
</plugin>
</plugins>
</pluginManagement>

You will then need to use the maven-dependency-plugin to copy the needed artifacts into the endorsed.dir:

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2</version>
</artifactItem>
<artifactItem>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2</version>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/endorsed</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

最后,你需要为CXF Codegen plugin做通用的安装配置,从而使其能够使用2.2的API及其运行时:

<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<configuration>
<fork>once</fork>
<additionalJvmArgs>-Djava.endorsed.dirs=${project.build.directory}/endorsed</additionalJvmArgs>
<!-- rest of the normal codegen configuration options -->
</configuration>
<dependencies>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</plugin>

其他配置项 Other configuration options

cxf-codegen-plugin 其他可能有用的配置:
| <fork>false/always/once</fork> | Forks a separate JVM for the code generation |
| ——————————– | —————————————- |
| <additionalJvmArgs>.... | Additional JVM args set on the forked process if fork is not false |
| <encoding>UTF-8</encoding> | (new in 2.6.1, requires configuring plugin to use very latest JAXB 2.2 impl jars) |

文章目录
  1. 1. Apache CXF 使用 - 工具 - Maven
    1. 1.1. 添加依赖
    2. 1.2. 额外依赖
    3. 1.3. Maven Snapshot Repository
    4. 1.4. Maven Java2WS 插件
    5. 1.5. Maven cxf-codegen-plugin (WSDL to Java)
      1. 1.5.0.1. 其他配置项 Other configuration options
|